package controle;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import controle.action.ActionCommand;
import controle.action.RankingListaAction;
import controle.action.VerificarAction;


@WebServlet("/ranking")
public class RankingSevlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	ActionCommand createCommand(HttpServletRequest request) {
		System.out.println("Entro no Servelet");
		String action = "nada";
		if (request.getParameter("action") != null)
			action = request.getParameter("action");
		
		System.out.println("Action: "+action );
		
		if ("VERIFICAR".equals(action)){
			System.out.println("Entro no Servelet VerificarAction");
			return new VerificarAction();
		}else{
			System.out.println("Entro no Servelet RankingListaAction");
			return new RankingListaAction();
		}
		
	}
	
	private void processRequest(HttpServletRequest request, HttpServletResponse response) {
		ActionCommand cmd = this.createCommand(request);
		if (cmd != null)
			cmd.executeAction(request, response);
		else
			try {
				response.sendError(HttpServletResponse.SC_BAD_REQUEST);
			} catch (IOException e) {
				e.printStackTrace();
			}
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.processRequest(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.processRequest(request, response);
	}


}
